package org.fessingapore.library.services;

import org.fessingapore.library.daos.ItemDao;
import org.fessingapore.library.models.Item;
import org.springframework.stereotype.Service;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.security.Security;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

@Service
public class ItemService extends GenericService<Item> {

private ItemDao itemDao;


// public static void main(String[] args) {
// 
//	 
//	 String jsonString = "";
//
//  //This is the request url  
//  String requestUrl = "https://www.googleapis.com/books/v1/volumes?q=isbn:9781565635227";
// // String requestUrl = "http://www.yahoo.com/";
//  
//  
//  try {
//   URL url = new URL(requestUrl.toString());
//   BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
//   
//   //URLConnection uc = url.openConnection();
//   //BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
//   String inputLine;
//
//   while ((inputLine = in.readLine()) != null) 
//       System.out.println(inputLine);
//   in.close();
//
//   while ((inputLine = in.readLine()) != null) {
//    //JSON data get stored as a string
//    jsonString = inputLine;
//
//   }
//   in.close();
//   /*
//   //the way to parse JSON array
//   JSONArray json = (JSONArray) JSONSerializer.toJSON(jsonString);
//   //getting another JSON string to parse
//   String parse1 = json.getString(0);
//   
//   //the way to parse JSON object
//   JSONObject json2 = (JSONObject) JSONSerializer.toJSON(parse1);
//   //getting a json array inside JSON object
//   JSONArray array1 = json2.getJSONArray("items");
//
//   for (int i = 0; i < array1.size(); i++) {
//    //getting a JSON object inside JSON array
//    JSONObject jBookRec = array1.getJSONObject(i);    
//    System.out.println("Book title= "+jBookRec.getString("title")+"\n"+"Authors= "+jBookRec.getJSONArray("authors").getString(0));
//    }
//*/
//  } catch (IOException e) {
//   e.printStackTrace();
//  }
// }
//
//
///*
//public static void main(String[] args) {
//    
//    if (args.length == 0) {
//      System.out.println("Usage: java HTTPSClient host");
//      return;
//    }       
//    
//    int port = 443; // default https port
//    String host = args[0];
//    
//    try {     
//      
//     Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
//      SSLSocketFactory factory 
//       = (SSLSocketFactory) SSLSocketFactory.getDefault();
//       
//      SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
//
//      Writer out = new OutputStreamWriter(socket.getOutputStream());
//      // https requires the full URL in the GET line
//      out.write("GET http://" + host + "/ HTTP/1.1\r\n");
//      out.write("\r\n");
//      out.flush(); 
//
//      // read response
//      BufferedReader in = new BufferedReader(
//       new InputStreamReader(socket.getInputStream()));
//      int c;
//      while ((c = in.read()) != -1) {
//        System.out.write(c);
//      }
//      
//      out.close();                  
//      in.close();
//      socket.close();
//      
//    } 
//    catch (IOException e) {
//      System.err.println(e);
//    }
//    
//  }
//  */
 

}



